package com.leo.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author du
 */
public class BioClient {
    public static void main(String[] args) throws IOException {

        //1、启动客户端连接
        Socket socket = new Socket("127.0.0.1", 6666);

        //2、监听控制台输入
        BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
        while (true) {
            //3、把控制台输入信息发送给服务端
            String sysinLine = sysin.readLine();
            if (sysinLine != null && !sysinLine.equals("")) {
                printWriter.println(sysinLine.trim());
                System.out.println("消息发送完毕");
            }
            //4、读取服务端发送过来的消息
            String readline = reader.readLine();
            if (readline != null && !readline.equals("")) {
                System.out.println("服务端响应: " + readline);
                if (readline.equals("bye")) {
                    System.out.println("关闭连接");
                    break;
                }
            }
        }
        socket.close();
    }
}
